import request from "@/utils/request";
import { getCurrentDate } from "@/utils/date";
import {
  getCurrentDateFormatted,
  getPreviousDay,
  formatCurrentDateTime,
  getLast12MonthsDates,
  formatCompactDate,
} from "@/utils/date";

// 登录接口
export function login() {
  return request({
    url: "/api-9000/prod-api/auth/ssoLogin",
    method: "post",
    data: {
      username: "admin",
      password: "123456",
    },
  });
}

const waterPublicUrl = (deviceId: number | string, size: number) =>
  `/api-9000/prod-api/iot/device/listThingsModel?deviceId=${deviceId}&pageNum=1&pageSize=${size}`;
const URLPublic =
  "/api-8090/getRealtimeData/getMonthlyIndicatorByYMDHMS?org_code=640000&";

// 获取水位  库容
export async function prodApi(deviceId: number) {
  return request({
    url: waterPublicUrl(deviceId, 1000),
    method: "get",
  });
}

// 获取水质
export async function waterApi(deviceId: number) {
  return request({
    url: waterPublicUrl(deviceId, 1000),
    method: "get",
  });
}

export async function waterV2API(deviceId: number, page?: number) {
  return request({
    url: waterPublicUrl(deviceId, 1000),
    method: "get",
  });
}

// 获取液位
export async function yeWeiApi(deviceId: number) {
  return request({
    url: waterPublicUrl(deviceId, 100),
    method: "get",
  });
}

// 获取天气预报
export async function getWeatherForecast() {
  return request({
    url: `/v2.6/Y2FpeXVuX25vdGlmeQ==/101.6656,39.2072/daily`,
    method: "get",
  });
}

export async function getCityLookup() {
  return request({
    url: `/geo/v2/city/lookup?location=银川`,
    method: "get",
  });
}

//   /getRealtimeData/getWaterSupplyInfo

// 获取   1 送水泵房总供水瞬时流量；2 送水泵房总供水当日累计流量；3 送水泵房总供水昨日累计流量 ；4 送水泵房总供水年累计流量；5 送水泵房总供水月累计流量
// export async function waterInfoApi(deviceId: number) {
//   let res = await login();
//   (window as any).token = 'Bearer ' + res.data
//   return request({
//     url: `/getRealtimeData/getWaterSupplyInfo`,
//     method: 'get',

//   });
// }

interface getWaterSupplyType {
  type: string;
  time?: string;
}

export async function getWaterSupply(params: getWaterSupplyType) {
  return request({
    url: `/api-8090/getRealtimeData/getWaterSupplyInfo?dataType=${params.type}&queryMonth=${params.time}`,
    method: "get",
  });
}

/**
 * 安全管理页面（左侧页面）
 */

// 风险总数 / 风险级别
export async function totalNumRisksAPICOPY() {
  const URL = `${URLPublic}index_frequency=yd&index_code=yjfx_yjfxdjs&data_dt=${getCurrentDate(
    "yyyy-MM"
  )}`;
  return request({
    url: URL,
    method: "get",
  });
}

// 应急预案总数
export async function emergencyPlanAPI() {
  const URL = `${URLPublic}index_frequency=day&index_code=yjya_yjyazs&data_dt=${getCurrentDate(
    "yyyy-MM"
  )}`;
  return request({
    url: URL,
    method: "get",
  });
}

//应急预案个数
export async function nummergencyPlansAPI() {
  const URL = `${URLPublic}index_frequency=day&index_code=yjya_yjyas&data_dt=${getCurrentDate(
    "yyyy-MM"
  )}`;
  return request({
    url: URL,
    method: "get",
  });
}

/**
 * 安全管理页面（右侧页面）
 */

// 安全隐患总件数
export async function totalHiddenDangerAPI() {
  const URL = `${URLPublic}index_frequency=yd&index_code=aqyhfx_aqyhs&data_dt=${getCurrentDate(
    "yyyy-MM"
  )}`;
  return request({
    url: URL,
    method: "get",
  });
}

// 安全隐患整改完成率
export async function rectificationRateAPI() {
  const URL = `${URLPublic}index_frequency=lj&index_code=aqyhfx_zzgwcl&data_dt=${getCurrentDate(
    "yyyy-MM"
  )}`;
  return request({
    url: URL,
    method: "get",
  });
}

// 安全隐患月整改完成率
export async function rectificationRateMonthAPI() {
  const URL = `${URLPublic}index_frequency=yd&index_code=aqyhfx_zgwcl&data_dt=${getCurrentDate(
    "yyyy"
  )}`;
  return request({
    url: URL,
    method: "get",
  });
}

//每月安全隐患整上报数量
export async function dangerReportsAPI() {
  const URL = `${URLPublic}index_frequency=yd&index_code=aqyhfx_aqyhs&data_dt=${getCurrentDate(
    "yyyy"
  )}`;
  return request({
    url: URL,
    method: "get",
  });
}

/**
 * 下线巡检
 */

// 线下巡检每月任务数
export async function taskNumAPI() {
  const URL = `${URLPublic}index_frequency=yd&index_code=xxxj_xjrws&data_dt=${getCurrentDate(
    "yyyy-MM"
  )}`;
  return request({
    url: URL,
    method: "get",
  });
}

// 下线巡检每月完成数
export async function taskNumCompleteAPI() {
  const URL = `${URLPublic}index_frequency=yd&index_code=xxxj_ywcrw&data_dt=${getCurrentDate(
    "yyyy-MM"
  )}`;
  return request({
    url: URL,
    method: "get",
  });
}

// 线下巡检未完成数
export async function taskNumUnCompleteAPI() {
  const URL = `${URLPublic}index_frequency=yd&index_code=xxxj_wwcrw&data_dt=${getCurrentDate(
    "yyyy-MM"
  )}`;
  return request({
    url: URL,
    method: "get",
  });
}

// 线下巡检每日已完成数
export async function taskNumDayCompleteAPI() {
  const URL = `${URLPublic}index_frequency=day&index_code=xxxj_ywcrw&data_dt=${getCurrentDate(
    "yyyy-MM-dd"
  )}`;
  return request({
    url: URL,
    method: "get",
  });
}

// 线下巡检完成率
export async function taskNumRateAPI() {
  const URL = `${URLPublic}index_frequency=yd&index_code=xxxj_xjwcl&data_dt=${getCurrentDate(
    "yyyy-MM"
  )}`;
  return request({
    url: URL,
    method: "get",
  });
}

/**
 * 设备管理页
 */

// 当月设备完好率接口
export async function equipmentManagementRateAPI() {
  const time = formatCompactDate(getPreviousDay(), "yyyy-MM-dd");
  const URLPrefix = `/api-8090/getRealtimeData/getDailyIndicatorByYMD?org_code=640000&`;
  const URL = `${URLPrefix}index_frequency=yd&index_code=sb_sbwhl&data_dt=${time}`;
  return request({
    url: URL,
    method: "get",
  });
}

// 重要设备完好率接口
export async function importantEquipmentManagementRateAPI() {
  const time = formatCompactDate(getPreviousDay(), "yyyy-MM-dd");
  const URLPrefix = `/api-8090/getRealtimeData/getDailyIndicatorByYMD?org_code=640000&`;
  const URL = `${URLPrefix}index_frequency=yd&index_code=sb_zysbwhl&data_dt=${time}`;
  return request({
    url: URL,
    method: "get",
  });
}

/**
 * 运行监测
 */
export async function getJinshuiAPI(id: number) {
  return request({
    url: `/api-8090/getRealtimeData/getWaterSupplyInfo?dataType=${id}`,
    method: "get",
  });
}

// 获取主控点压力
export async function getZhuKongDianAPI() {
  const time = formatCompactDate(getPreviousDay(), "yyyy-MM-dd");
  const URLPrefix = `/api-8090/getRealtimeData/getDailyIndicatorByYMDHMS?org_code=640000&`;
  const URL = `${URLPrefix}index_frequency=day&index_code=yl_zkdyl_avg&data_dt=${time}`;
  return request({
    url: URL,
    method: "get",
  });
}

// 获取工况
export async function getGongKuangAPI() {
  const time = formatCompactDate(getPreviousDay(), "yyyy-MM-dd");
  const URLPrefix = `/api-8090/getRealtimeData/getDailyIndicatorByYMD?org_code=640000&`;
  const URL = `${URLPrefix}index_frequency=day&index_code=sb_gjsl&data_dt=${time}`;
  return request({
    url: URL,
    method: "get",
  });
}

export async function getNengHaoAPI() {
  const time = formatCompactDate(getPreviousDay(), "yyyy-MM-dd");
  const URLPrefix = `/api-8090/getRealtimeData/getDailyIndicatorByYMDHMS?org_code=640000&`;
  const URL = `${URLPrefix}index_frequency=day&index_code=xl_sbxl_ddh&data_dt=${getCurrentDate(
    "yyyy-MM-dd"
  )}`;
  return request({
    url: URL,
    method: "get",
  });
}

export async function waterInfoAPI(reg) {
  const URL = `/api-8090/getRealtimeData/getWaterSupplyInfo?${new URLSearchParams(
    reg
  ).toString()}`;
  return request({
    url: URL,
    method: "get",
  });
}

// 获取当月送水泵房最佳水量
export async function waterBestAPI() {
  const URL = `/api-8090/getRealtimeData/selectHistoryIndicator?queryDate=${getCurrentDate(
    "yyyy"
  )}&queryItem=月最佳出水效率`;
  return request({
    url: URL,
    method: "get",
  });
}

export async function getWaterDataAPI(reg) {
  const URL = `/api-8090/getRealtimeData/getWaterQualityInfo?${new URLSearchParams(
    reg
  ).toString()}`;
  return request({
    url: URL,
    method: "get",
  });
}

/**
 * 设备维修指标编码常量
 */
const INDEX_CODE = {
  yiwexiu: "sbwx_ywxsbs", // 已维修
  daiweixiu: "sbwx_dwxsbs", // 待维修
  yiyanshou: "sbwx_yysrws", // 已验收
  weixiurenwu: "sbwx_sbwxrws", // 维修任务
};

/**
 * 通用指标请求函数
 * @param indexCode 指标编码
 * @param dateFormat 日期格式（年/月/日）
 */
function getIndicatorData(
  indexCode: string,
  dateFormat: "yyyy-MM-dd" | "yyyy-MM" | "yyyy"
) {
  const date = formatCompactDate(getPreviousDay(), dateFormat);
  const url = `/api-8090/getRealtimeData/getDailyIndicatorByYMD?org_code=640000&index_frequency=yd&index_code=${indexCode}&data_dt=${date}`;
  return request({ url, method: "get" });
}

// 今日数据（按天）
export const getYweixiuAPI = () =>
  getIndicatorData(INDEX_CODE.yiwexiu, "yyyy-MM-dd");
export const getDweixiuAPI = () =>
  getIndicatorData(INDEX_CODE.daiweixiu, "yyyy-MM-dd");
export const getYyanshouAPI = () =>
  getIndicatorData(INDEX_CODE.yiyanshou, "yyyy-MM-dd");
export const getYRenwuAPI = () =>
  getIndicatorData(INDEX_CODE.weixiurenwu, "yyyy-MM-dd");

// 本月数据（按月）
export const getMonthRepairedAPI = () =>
  getIndicatorData(INDEX_CODE.yiwexiu, "yyyy-MM");
export const getMonthPendingRepairAPI = () =>
  getIndicatorData(INDEX_CODE.daiweixiu, "yyyy-MM");
export const getMonthAcceptedAPI = () =>
  getIndicatorData(INDEX_CODE.yiyanshou, "yyyy-MM");
export const getMonthTaskAPI = () =>
  getIndicatorData(INDEX_CODE.weixiurenwu, "yyyy-MM");

// 本年数据（按年）
export const getYearRepairedAPI = () =>
  getIndicatorData(INDEX_CODE.yiwexiu, "yyyy");
export const getYearPendingRepairAPI = () =>
  getIndicatorData(INDEX_CODE.daiweixiu, "yyyy");
export const getYearAcceptedAPI = () =>
  getIndicatorData(INDEX_CODE.yiyanshou, "yyyy");
export const getYearTaskAPI = () =>
  getIndicatorData(INDEX_CODE.weixiurenwu, "yyyy");

// 查询设备信息
export async function getEquipmentMaintainInfoAPI() {
  // const time = formatCompactDate(getPreviousDay(), "yyyy-MM-dd");
  const time = getCurrentDate("yyyy-MM-dd");
  const URL = `/api-8090/getRealtimeData/getEquipmentMaintainInfo?data_dt=${time}`;
  return request({
    url: URL,
    method: "get",
  });
}

// 获取设备完成率
export async function getEquipmentMaintainRateAPI() {
  const time = getCurrentDate("yyyy");
  const URL = `/api-8090/getRealtimeData/getDailyIndicatorByYMD?index_frequency=day&index_code=sbwh_jbnwhzwcl&org_code=640000&data_dt=${time}`;
  return request({
    url: URL,
    method: "get",
  });
}

// 获取近半年维护总次数
export async function getEquipmentMaintainCountAPI() {
  const time = getCurrentDate("yyyy");
  const URL = `/api-8090/getRealtimeData/getDailyIndicatorByYMD?index_frequency=day&index_code=sbwh_jbnwhzcs&org_code=640000&data_dt=${time}`;
  return request({
    url: URL,
    method: "get",
  });
}

// 获取周供水量
export async function getWeekSupplyWaterAPI() {
  const time = getCurrentDate("yyyy-MM-dd");
  console.log(time);

  const URL = `/api-8090/getRealtimeData/selectHistoryIndicator?queryDate=${time}&queryItem=周供水量`;
  return request({
    url: URL,
    method: "get",
  });
}

// 获取药耗剩余时长
export async function getWaterSupplyAPI() {
  const URL = `/api-8090/getRealtimeData/getPACRemainingDuration`;
  return request({
    url: URL,
    method: "get",
  });
}

export function getNaCloAPI() {
  const URL = `/api-8090/getRealtimeData/selectNACIORemainingDuration?queryDay=${getCurrentDateFormatted()}`;
  return request({
    url: URL,
    method: "get",
  });
}

// 线上巡检
export async function getOnlineInspectionAPI() {
  const reg = {
    inspectionRoute: "xsxj-5704e26b1e22470489f3bafdfc3e176e-1736996030297",
    deviceDataList: [
      {
        equipId: "1",
        equipName: "设备1",
        dataType: "abc",
        dataVal: 33,
      },
      {
        equipId: "2",
        equipName: "设备2",
        dataType: "abd",
        dataVal: 76,
      },
    ],
    taskStartTime: "1975-08-20 14:20:35",
    taskEndTime: "1974-11-25 05:42:48",
  };
  const URL = `/api-8090/xjjlInspect/getData`;
  return request({
    url: URL,
    method: "get",
  });
}

/**
 * 实时雨晴登录接口
 */
export async function getRainfallLoginAPI() {
  return request({
    data: {
      name: "user2",
      password: "746fb6e1f22c785e298bc96836c3992f",
    },
    url: `/api-8000/bin/login.wk`,
    method: "post",
  });
}

/**
 * 实时雨晴数据
 */
export async function getRainfallDataAPI() {
  const URL = `/api-8000/bin/getCurrentValue.wk`;
  return request({
    data: [
      "Net1.长城水务水雨情.水雨情@5分钟雨量",
      "Net1.长城水务水雨情.水雨情@当前雨量",
      "Net1.长城水务水雨情.水雨情@降水累积量",
      "Net1.长城水务水雨情.水雨情@水位",
      "Net1.长城水务水雨情.水雨情@温度",
      "Net1.长城水务水雨情.水雨情@湿度",
      "Net1.长城水务水雨情.水雨情@风速",
      // "Net1.长城水务水雨情.水雨情@风向",
      "Net1.长城水务水雨情.水雨情@光照",
      // "Net1.长城水务水雨情.水雨情@日累计辐射",
      // "Net1.长城水务水雨情.水雨情@PM25"
    ],
    url: URL,
    method: "post",
  });
}
